package com.glela.controller;

import com.alibaba.fastjson.JSON;
import com.glela.cache.model.RedisKey;
import com.glela.common.util.JsonUtil;
import com.glela.live.model.LiveRoomFollow;
import com.glela.live.service.LiveRoomFollowService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.request.RequestPageBaseModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponsePageModel;
import com.glela.platform.util.ParameterUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/liveRoomFollow")
public class LiveRoomFollowController extends LiveBaseController{
	
	private static Logger logger = LoggerFactory.getLogger(LiveRoomFollowController.class);

	
    @Autowired
    private LiveRoomFollowService liveRoomFollowService;

    /**
     * 添加关注直播
     * @param liveRoomFollow
     * @return
     */
    @RequestMapping(value = "/addLiveRoomFollow",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> addLiveRoomFollow(@RequestBody LiveRoomFollow liveRoomFollow){
        try {
        	String redisResult = redisUtil.lget(RedisKey.REDIS_KEY_LIVE_ROOM_FOLLOW_LIST+liveRoomFollow.getUserId(),liveRoomFollow.getLiveUserId());
            if (StringUtils.isNotBlank(redisResult)){
    			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
            }
            LiveRoomFollow result = liveRoomFollowService.addLiveRoomFollow(liveRoomFollow);
            if (result!=null){
                redisUtil.lpush(RedisKey.REDIS_KEY_LIVE_ROOM_FOLLOW_LIST+liveRoomFollow.getUserId(),result.getLiveUserId());
    			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
            }
            //添加关注直播失败 
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C90004), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("添加关注直播失败", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
    }

    /**
     * 删除关注直播
     * @param liveRoomFollow
     * @return
     */
    @RequestMapping(value = "/delLiveRoomFollow",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> delLiveRoomFollow(@RequestBody LiveRoomFollow liveRoomFollow){
    	   try {
    		   int count = liveRoomFollowService.delLiveRoomFollow(liveRoomFollow);
    	        if (count>0){
    	            redisUtil.lrem(RedisKey.REDIS_KEY_LIVE_ROOM_FOLLOW_LIST+liveRoomFollow.getUserId(),0,liveRoomFollow.getLiveUserId());
        			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
    	        }//删除关注直播失败
    			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C90005), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除关注直播失败", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		
		}
    }

   /* *//**
     * 获取关注直播列表
     * @param requestPageBaseModel
     * @return
     *//*
    @RequestMapping(value = "/getPageList",method = RequestMethod.POST,consumes = "application/json")
    public String getPageList(@RequestBody RequestPageBaseModel requestPageBaseModel){
        if (ParameterUtils.validParams(requestPageBaseModel.getUserId(), requestPageBaseModel.getPageNum(), requestPageBaseModel.getPageSize())) {
            return JsonUtil.getFailJsonStr("缺少必传参数");
        }
        int end = requestPageBaseModel.getStartIndex()+requestPageBaseModel.getPageSize()-1;
        List<String> list = redisUtil.lrange(RedisKey.REDIS_KEY_LIVE_ROOM_FOLLOW_LIST+requestPageBaseModel.getUserId(),requestPageBaseModel.getStartIndex(),end);
        List<LiveRoom> result = new ArrayList<>();
        if (list!=null && list.size()>0){
            for (String str:list) {
                String redisResult = redisUtil.get(RedisKey.REDIS_KEY_LIVEROOM_MODEL+str);
                if (StringUtils.isNotBlank(redisResult)){
                    LiveRoom liveRoom = JSON.parseObject(redisResult,LiveRoom.class);
                    String status = redisUtil.get(RedisKey.REDIS_KEY_LIVE_STATUS+liveRoom.getLiveUserId());
                    liveRoom.setStatus(StringUtils.isNotBlank(status)?Integer.valueOf(status):0);
                    result.add(liveRoom);
                }else{
                    LiveRoom liveRoom = liveRoomService.getLiveRoomByUid(Long.valueOf(str));
                    redisUtil.set(RedisKey.REDIS_KEY_LIVEROOM_MODEL+str,liveRoom);
                    result.add(liveRoom);
                }
            }
        }
        Long count = redisUtil.llength(RedisKey.REDIS_KEY_LIVE_ROOM_FOLLOW_LIST+requestPageBaseModel.getUserId());
        ResponsePageModel responsePageModel = new ResponsePageModel<LiveRoom>(count.intValue(), requestPageBaseModel.getPageSize(), requestPageBaseModel.getPageNum(), result);
        return JsonUtil.getSuccessJsonStr("ok",responsePageModel);
    }*/
}
